﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using RN.ERP.Entity;
using RN.ERP.Entity.ZBDLRY;
using System.Data.Entity;
using Fly.Log;
using RN.ERP.DAL.Context;


namespace RN.ERP.DAL.LcDAL
{
    public class ChangeDALWh
    {
        ZBDL_Context dbContext = new ZBDL_Context();

        public ChangeDALWh(ZBDL_Context dbContext)
        {
            this.dbContext = (ZBDL_Context)dbContext;
        }

        public IList<V_ChangeInfoEx> GetChanges(string objectType, string corpUserGuid, string flowCode, string condition, int pageIndex, int pageSize, out int count)
        {
            string strSql = "select * from v_changeinfoEx where isdel=0 ";
            if (!string.IsNullOrEmpty(objectType) && !objectType.Equals("0"))
            {
                if (objectType.Equals("1"))
                {
                    strSql += "and objecttype in('1','3') ";
                }
                else if (objectType.Equals("4"))
                {
                    strSql += "and objecttype in('4','6') ";
                }
                else
                {
                    strSql += "and objecttype='" + objectType + "' ";
                }
            }
            if (!string.IsNullOrEmpty(corpUserGuid))
            {
                strSql += " and isrtUserId=@isrtUserId ";
            }
            if (!string.IsNullOrEmpty(flowCode))
            {
                strSql += " and flowcode=@flowcode ";
            }
            if (!string.IsNullOrEmpty(condition))
            {
                strSql += condition;
            }
            strSql += "  ORDER BY ObjectType,IsrtDate desc ";
            count = dbContext.Database.SqlQuery<V_ChangeInfoEx>(strSql, new SqlParameter[] { 
                new SqlParameter("isrtUserId",corpUserGuid),
                new SqlParameter("flowcode",flowCode)
            }).ToList().Count;
            return dbContext.Database.SqlQuery<V_ChangeInfoEx>(strSql, new SqlParameter[] { 
                new SqlParameter("isrtUserId",corpUserGuid),
                new SqlParameter("flowcode",flowCode)
            }).ToList().Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        public bool DeleteChanges(int changeSetId)
        {
            string strSql = "delete from tb_changeRecordEx where changesetid=@changeSetId ";
            dbContext.Database.ExecuteSqlCommand(strSql, new SqlParameter("changeSetId", changeSetId));
            return true;
        }
        /// <summary>
        /// 获取指定变更集的详细信息，包括变更记录，变更规则，审核状态等
        /// </summary>
        /// <param name="changeSetId"></param>
        /// <returns></returns>
        public List<ChangeItemRecordInfoEx> GetChangeItemRecordInfo(int changeSetId,string appId)
        {
            return dbContext.Database.SqlQuery<ChangeItemRecordInfoEx>(@"SELECT  
r.ID AS RecordID,
r.EmpGuid,
r.ChangeSetID,
r.BeforeChange,
r.AfterChange,
r.Remark,
r.OptDate,
r.BeforeValue,
r.AfterValue,
f.FlowCode,
s.ObjectId,
i.ID AS ItemID,
i.ClassID,
i.ChangeItem,
i.ChangeItemName,
i.ClassValue,
i.RegularExpression,
i.UpdateTable,
i.UpdateColumn,
i.SortCode,
i.BindSQL,
i.UploadID,
i.IsDel,
i.ItemType,
i.BeforeSQL, 
i.SelectType
FROM    dbo.Tb_ChangeSetRecordEx s 
INNER JOIN dbo.Tb_FlowCode f ON cast(s.Id as nvarchar(50)) = f.ObjectId AND f.CodeType = 8020 AND  f.IsDel <> 1  
INNER JOIN dbo.tb_ChangeRecordEx  r ON r.ChangeSetID=s.Id 
INNER JOIN dbo.tb_ChangeItemEx i ON r.ChangeItem=i.ChangeItem WHERE s.Id=@SetID AND i.IsDel <> 1  and i.classid=8020 and i.appid=@appId ", new SqlParameter[] { new SqlParameter("@SetID", changeSetId), new SqlParameter("@appId", appId) }).ToList();
        }

        public List<User_Log> AuditLogs(string objectId,string appId)
        {
            List<User_Log> logs = null;
            if (!string.IsNullOrWhiteSpace(objectId))
            {
                string sql = string.Format("SELECT * FROM User_Log WHERE ObjectId='{0}' AND Code='8020' and AppID='{1}' AND (FunctionId is not Null AND FunctionId>2)  order BY Id desc ", objectId.Trim(),appId);
                IEnumerable<User_Log> list = dbContext.Database.SqlQuery<User_Log>(sql, new SqlParameter[] { });
                if (list != null)
                {
                    logs = list.ToList();
                }

            }
            return logs;
        }
    }
}
